package net.flowas.genericcrud.audit.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;

import net.flowas.genericcrud.ExecuteContext;
import net.flowas.genericcrud.audit.ActionType;
import net.flowas.genericcrud.audit.AuditService;
import net.flowas.genericcrud.service.chain.AbstractExecuteHandler;
import net.flowas.genericcrud.service.chain.ExecuteHandler;

public class AuditExecuteHandler extends AbstractExecuteHandler{
	private AuditService auditService;	
	
	public AuditExecuteHandler(ExecuteHandler next) {
		super(next);
	}
	
		
    public void setAuditService(AuditService auditService) {
		this.auditService = auditService;
	}

	@Override
    public void handleRequest(ExecuteContext req) {
    	super.handleRequest(req);
    	ActionType action = null;
    	Long id = null;
    	switch (req.getAction()) {
		case GET:
			action=ActionType.BROWSE;
			id=(Long) req.getQueryOption().getExample();
			break;
		case DELETE:
		    action=ActionType.REMOVED;
		    id=(Long) req.getQueryOption().getExample();
		    break;
		default:
			break;
		}
    	if(action!=null && id !=null) {
    		auditService.mark(req.getModelClass().getSimpleName(), action, id, null);
    	}
    }


	@Override
	public Collection<Class<? extends Serializable>> getHandledTypes() {
		return Collections.emptySet();
	}
}
